const rootCategory = require('../model/root_category')

module.exports = {

  /**
   * 获取root类目信息
   * @param request
   * @param response
   * @param next
   */
  gRootCategory: (request, response, next) => {
    rootCategory.getRootCategory()
        .then(result => {
          request.root_navs = result
          next()
        })
        .catch(error => {
          next(error)
        })
  },

  /**
   * 插入root类目中间件
   * @param request
   * @param response
   * @param next
   */
  sRootCategory: (request, response, next) => {
    let {name, describe} = request.body
    let root_category = {
      name,
      describe
    }

    rootCategory.setRootCategory(root_category)
      .then(res => {
        request.add_rcid = res
        next()
      })
      .catch(error => {
        next(error)
      })
  },

  /**
   * 删除主类目中间件
   * @param request
   * @param response
   * @param next
   */
  dRootCategory: (request, response, next) => {
    let {id} = request.body
    rootCategory.deleteRootCategory(id)
      .then(result => {
        request.affectR = result
        next()
      })
      .catch(error => {
        next(error)
      })
  },

  /**
   * 更新主类目的中间件
   * @param request
   * @param response
   * @param next
   */
  uRootCategory: (request, response, next) => {
    let {root_category} = request.body
    rootCategory.updateRootCategory(root_category)
      .then(result => {
        request.affectR = result
        next()
      })
      .catch(error => {
        next(error)
      })
  }
}
